package org.jeecg.modules.system.util;


import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.aspect.annotation.ChangeLog;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.asset.entity.AssetMessageChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ChangeLogUtil {

    /**
     * 比较两个对象的字段变化
     * @param oldObj 变更前对象
     * @param newObj 变更后对象
     * @return 字段变化记录
     */
    public static List<AssetMessageChange> compareChanges(Object oldObj, Object newObj, String pid) {
        List<AssetMessageChange> changes = new ArrayList<>();

        if (oldObj == null || newObj == null) {
            return changes;
        }

        Field[] fields = oldObj.getClass().getDeclaredFields();

        for (Field field : fields) {
            if (field.isAnnotationPresent(ChangeLog.class)) {
                try {
                    field.setAccessible(true);
                    Object oldValue = field.get(oldObj);
                    Object newValue = field.get(newObj);
                    // 规范化值用于比较
                    String oldStrValue = normalizeValue(oldValue);
                    String newStrValue = normalizeValue(newValue);

                    boolean hasChanged = false;
                    if (oldStrValue == null && newStrValue != null) {
                        hasChanged = true;
                    } else if (oldStrValue != null && newStrValue == null) {
                        hasChanged = true;
                    } else if (oldStrValue != null && newStrValue != null && !oldStrValue.equals(newStrValue)) {
                        hasChanged = true;
                    }
                    if (hasChanged) {
                        ChangeLog annotation = field.getAnnotation(ChangeLog.class);
                        String fieldName = annotation.value().isEmpty() ? field.getName() : annotation.value();
                        AssetMessageChange assetMessageChange = new AssetMessageChange();
                        assetMessageChange.setChangeField(field.getName());
                        assetMessageChange.setChangeFieldName(fieldName);
                        assetMessageChange.setOldValue(oldStrValue == null ? "" : oldStrValue);
                        assetMessageChange.setNewValue(newStrValue == null ? "" : newStrValue);
                        assetMessageChange.setPid(pid);
                        changes.add(assetMessageChange);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        return changes;
    }

    /**
     * 规范化值，处理数字类型的小数点问题
     * @param value 原始值
     * @return 规范化后的字符串表示
     */
    private static String normalizeValue(Object value) {
        if (value == null) {
            return null;
        }

        String strValue = value.toString();

        // 处理数字类型，移除小数点后无意义的0
        if (strValue.matches("-?\\d+(\\.\\d+)?")) {
            try {
                // 尝试解析为数字并格式化
                double num = Double.parseDouble(strValue);
                // 如果是整数，返回整数形式
                if (num == (long) num) {
                    return String.valueOf((long) num);
                }
                // 否则移除末尾无意义的0
                return String.valueOf(num).replaceAll("0*$", "").replaceAll("\\.$", "");
            } catch (NumberFormatException e) {
                // 解析失败则返回原值
                return strValue;
            }
        }

        return strValue;
    }
}
